﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ArgusPCLib.Diagnostics
{
	public static class Process
	{
		static Type _processType;

		static Process()
		{
			Assembly assembly = null;
			AssemblyName assemblyName = new AssemblyName("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
			try { assembly = Assembly.Load(assemblyName); }
			catch (Exception exception)
			{
				Bird.Logger.LogExceptionThrown(Bird.LogVerbosities.Warnings, exception);
				return;
			}
			Process._processType = assembly.GetType("System.Diagnostics.Process");
		}

		public static bool IsAvailable { get { return Process._processType != null; } }

		public static bool Start(string fileName)
		{
			if (!Process.IsAvailable)
			{
				Bird.Logger.LogMessage(Bird.LogVerbosities.Warnings, "Process.IsAvailable returns false.");
				return false;
			}

			MethodInfo mi = Process._processType.GetRuntimeMethod("Start", new Type[] { typeof(string) });
			mi.Invoke(null, new object[] { fileName });
			return true;
		}

		public static bool Start(string fileName, string arguments)
		{
			if (!Process.IsAvailable)
			{
				Bird.Logger.LogMessage(Bird.LogVerbosities.Warnings, "Process.IsAvailable returns false.");
				return false;
			}

			MethodInfo mi = Process._processType.GetRuntimeMethod("Start", new Type[] { typeof(string), typeof(string) });
			mi.Invoke(null, new object[] { fileName, arguments});
			return true;
		}

		public static bool OpenExplorerAndSelect(string path)
		{
			return Process.Start("explorer.exe", string.Format("/select,\"{0}\"", path));
		}
	}
}
